package com.youlin.untils;

import com.youlin.entities.BygAppointment;
import java.time.LocalTime;
import java.util.List;

public class MyTimeUtil {

    private MyTimeUtil() {
        throw new IllegalStateException("Utility class");
    }

    /**
     * 校验 startTime-endTime 是否与 appointments 中的任意一个 appointment的startTime-endTime 时间段重合
     * @param appointments
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean checkAmUseTime(List<BygAppointment> appointments, LocalTime startTime, LocalTime endTime) {
        return appointments.parallelStream().anyMatch(am -> checkTime(am.getStartTime(), am.getEndTime(), startTime, endTime));
    }

    /**
     * 校验 ts1-te1 是否与 ts2-te2 时间段有重合
     * @param ts1
     * @param te1
     * @param ts2
     * @param te2
     * @return
     */
    public static boolean checkTime (LocalTime ts1, LocalTime te1, LocalTime ts2, LocalTime te2) {
        return checkTimeEach(ts1, te1, ts2, te2) || checkTimeEach(ts2, te2, ts1, te1);
    }
    private static boolean checkTimeEach (LocalTime ts1, LocalTime te1, LocalTime ts2, LocalTime te2) {
        return ts2.isBefore(te1) && ts2.isAfter(ts1) || te2.isAfter(ts1) && te2.isBefore(te1)
                || ts2.isBefore(ts1) && te2.isAfter(te1) || ts2.isAfter(ts1) && te1.isBefore(te1)
                || ts1.equals(ts2) && te1.equals(te2);
    }

}
